const express = require("express");
const router = express.Router();
const { success, successWithData } = require("../utils/result");
const { check } = require("express-validator");
const { checkParam } = require("../utils/checkUtils");
const FrontRequestLog = require("../db/model/frontRequestLogModel");

/**
 * 分页查询接口
 * @api {POST} /frontRequestLog/queryFrontRequestLogListByPage 分页查询接口
 * @apiDescription 分页查询接口
 * @apiName queryFrontRequestLogListByPage
 * @apiHeader {String} authorization 用户token,格式：Bearer xxxx
 * @apiBody {Number} page=1   页码
 * @apiBody {Number} limit=10   每页条数
 * @apiParamExample {json} Request-Example:
 *     {
 *        "page": 1,
 *        "limit": "10",
 *     }
 * @apiGroup FrontRequestLog
 * @apiVersion 1.0.0
 */
router.post(
  "/frontRequestLog/queryFrontRequestLogListByPage",
  check("page").not().isEmpty(),
  check("limit").not().isEmpty(),
  async function (req, res) {
    checkParam(req);
    const { page, limit, user_id } = req.body;
    const where = {};
    let offset = (page - 1) * limit;
    if (user_id) {
      where["user_id"] = user_id;
    }
    const result = await FrontRequestLog.findAndCountAll({
      limit,
      offset,
      where,
      order: [["created_time", "DESC"]],
    });
    return res.json(successWithData(result));
  }
);

/**
 * @api {POST} /frontRequestLog/add 添加
 * @apiDescription 添加
 * @apiName add
 * @apiHeader {String} authorization 用户token,格式：Bearer xxxx
 * @apiBody {Number} id=1   主键
 * @apiBody {Number} user_id=1   用户id
 * @apiBody {String} url='请求地址'   请求地址
 * @apiBody {String} method='请求方式'   请求方式
 * @apiBody {String} body='请求body'   请求body
 * @apiBody {String} params='请求params'   请求params
 * @apiBody {String} ip_address='ip地址'   ip地址
 * @apiBody {String} result='响应内容'   响应内容
 * @apiBody {String} created_time='1994-04-01'   创建时间
 * @apiBody {String} updated_time='1994-04-01'   更新时间
 * @apiParamExample {json} Request-Example:
 *     {
 *        "id": 1,
 *        "user_id": 1,
 *        "url": '请求地址',
 *        "method": '请求方式',
 *        "body": '请求body',
 *        "params": '请求params',
 *        "ip_address": 'ip地址',
 *        "result": '响应内容',
 *        "created_time": '1994-04-01',
 *        "updated_time": '1994-04-01',
 *     }
 * @apiGroup FrontRequestLog
 * @apiVersion 1.0.0
 */
router.post("/frontRequestLog/add", async function (req, res) {
  await FrontRequestLog.create({ ...req.body });
  return res.json(success());
});

/**
 * @api {POST} /frontRequestLog/edit 编辑
 * @apiDescription 编辑
 * @apiName edit
 * @apiHeader {String} authorization 用户token,格式：Bearer xxxx
 * @apiBody {Number} id=1   主键
 * @apiBody {Number} user_id=1   用户id
 * @apiBody {String} url='请求地址'   请求地址
 * @apiBody {String} method='请求方式'   请求方式
 * @apiBody {String} body='请求body'   请求body
 * @apiBody {String} params='请求params'   请求params
 * @apiBody {String} ip_address='ip地址'   ip地址
 * @apiBody {String} result='响应内容'   响应内容
 * @apiBody {String} created_time='1994-04-01'   创建时间
 * @apiBody {String} updated_time='1994-04-01'   更新时间
 * @apiParamExample {json} Request-Example:
 *     {
 *        "user_id": 1,
 *        "url": '请求地址',
 *        "method": '请求方式',
 *        "body": '请求body',
 *        "params": '请求params',
 *        "ip_address": 'ip地址',
 *        "result": '响应内容',
 *        "created_time": '1994-04-01',
 *        "updated_time": '1994-04-01',
 *     }
 * @apiGroup FrontRequestLog
 * @apiVersion 1.0.0
 */
router.post(
  "/frontRequestLog/edit",
  check("id").not().isEmpty(),
  async function (req, res) {
    checkParam(req);
    await FrontRequestLog.update(
      { ...req.body },
      { where: { id: req.body.id } }
    );
    return res.json(success());
  }
);

/**
 * @api {POST} /frontRequestLog/del 删除
 * @apiDescription 删除
 * @apiName del
 * @apiHeader {String} authorization 用户token,格式：Bearer xxxx
 * @apiBody {Number} id=1   主键id
 * @apiParamExample {json} Request-Example:
 *     {
 *        "id": 1,
 *     }
 * @apiGroup FrontRequestLog
 * @apiVersion 1.0.0
 */
router.post(
  "/frontRequestLog/del",
  check("id").not().isEmpty(),
  async function (req, res) {
    checkParam(req);
    await FrontRequestLog.destroy({ where: { id: req.body.id } });
    return res.json(success());
  }
);
module.exports = router;
